home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3491 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: New C Programmer Has A Problem
  5. Date: 29 Jan 1996 13:28:15 GMT
  6. Organization: Customer of InfiNet
  7. Message-ID: <4eii1f$5il@nw002.infi.net>
  8. References: <4ehpa3$6kl@nntp.novia.net>
  9. Reply-To: nngis@norfolk.infi.net
  10. NNTP-Posting-Host: h-skeletoncrew.norfolk.infi.net
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.99.3
  13.  
  14. In article <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net says...
  15. >
  16. >
  17. > START MESSAGE:
  18. >  I have a question on C... why isn't this program working?!
  19. >  
  20. >  Program START:
  21. >  /* File Open */
  22. >  
  23. >  #include <clib/dos_protos.h>
  24. >  #include <dos/dos.h>
  25. >  #include <stdio.h>
  26. >  #include <exec/types.h>
  27. >  
  28. >  void main();
  29. >  
  30. >    char *name;
  31. >    int age;
  32. >            
  33. >  void main()
  34.  
  35. *** First watch out for the ANSI/ISO police on typing "main" to not 
  36. *** return a value. You might try "int main()".
  37.  
  38. >  
  39. >  {
  40. >    struct FileHandle *file_handle;
  41. >    long bytes_written;
  42. >    long bytes_read;
  43. >  
  44. >   printf("Enter your name: ");
  45. >   scanf("%s",name);
  46.  
  47. *** Second, you are reading input into an undefined area of memory
  48. *** in that you have not set "*name" to point to anything. You could
  49. *** be overwriting anything at this point. Very dangerous! You should
  50. *** be using a buffer, not an undefined pointer. For example,
  51. ***      char name[80];
  52.  
  53. >   printf("\nEnter your age: ");  scanf("%d",age);
  54.  
  55. *** Third, (you're new to 'C', aren't you?) you must precede variables
  56. *** in a "scanf" with the "&" operator to tell "scanf" where to put
  57. *** the input value. This does not,however, apply to arrays and pointers,
  58. *** but you're using an "int" so you need to do it. That is, recode
  59. *** "scanf("%d",age);" as "scanf("%d",&age);". The way you have coded it
  60. *** you are again overwriting memory somewhere, but you are not putting
  61. *** a value in "age" (more than likely). You are putting a value in the
  62. *** memory address that corresponds to the value integer value contained
  63. *** in "age", not in "age" itself.
  64.  
  65. *** My eyes are getting tired, so I'll leave the rest for somebody else.
  66. *** Believe me, there is enough here to keep you busy for awhile.
  67.  
  68. >  ... Snipped ...
  69.  
  70. Hope this helps,
  71. Greg DiGiorgiop
  72.  
  73.